<?php 
/**
  * 评论控制器
  * date 2013-1-17 
  */
class CommentAction extends CommonAction {

	public function _initialize()
	{
    $this->currentModel = D(MODULE_NAME);
		parent::_initialize();
    $this->assign('sourceArr',C('MODELTYPE'));		
	}

  public function index()
  {
    $map=$this->_search();
    $map['parent_id']=0;
    $this->_filter($map);
    $count=$this->currentModel->where($map)->count();
    $page=$this->_page($count,C('PAGE_SUM'));
    $datas=$this->currentModel->where($map)->limit($page->firstRow.','.$page->listRows)->order($this->pkId.' DESC')->select();
    $this->assign('page',$page->show());
    $this->assign('datas',$datas);       
    $this->display();
  }

  public function add()
  {
    if($this->isPost())
    {
      $data=$this->currentModel->create();
      if($data===FALSE)
      {
        $this->error($this->currentModel->getError());
      }
      $data['email']=$_SESSION['email'];
      $data['status']=1;
      $data['username']=$_SESSION['truename'];
      $func='save';
      if($this->_post('reply_id')==0)
      {
        $func='add';
      }else{$data['id']=$this->_post('reply_id');}      
      if($this->currentModel->$func($data)!==false)
      {
        $this->currentModel->resume(array('id'=>$_POST['parent_id']));
        $this->success('成功',U(MODULE_NAME.'/index'));
      }else
          {
            $this->error('失败');
          }      
    }else
        {
          $id=$this->_get($this->pkId);
          $data=$this->currentModel->find($id);
          if(empty($data))$this->error('参数违法',U(MODULE_NAME.'/index'));
          $sourceData=M($data['source'])->field('title,id')->find($data['value_id']);
          $replyData=$this->currentModel->where('parent_id='.$data['id'])->find();
          $this->assign('sourceData',$sourceData);
          $this->assign('replyData',$replyData);
          $this->assign('data',$data);
          $this->display('add');
        }
  }
    

  /**
    * 过滤搜索条件
    * @access private
    * @param array &$map
    */
  private function _filter(&$map)
  {
    if(isset($_GET['value_id']))
    {
      $map['value_id']=$this->_get('value_id'); 
    }
    $this->assign('param',$map);
    if(isset($map['username']))
    {
      $map['username']=array('like',"%".$map['username']."%");
    }
    if((isset($_REQUEST['start_time'])&&$_REQUEST['start_time']>0)&&(isset($_REQUEST['end_time'])&&$_REQUEST['end_time']>0))
    {
      $map['add_time']=array('BETWEEN',strtotime($_REQUEST['start_time']).','.strtotime($_REQUEST['end_time']));
    }elseif(isset($_REQUEST['start_time'])&&$_REQUEST['start_time']>0)
    {
      $map['add_time']=array('EGT',strtotime($_REQUEST['start_time']));
    }elseif(isset($_REQUEST['end_time'])&&$_REQUEST['end_time']>0)
    {
      $map['add_time']=array('ELT',strtotime($_REQUEST['end_time']));
    }         
  }

  /**
  * 改变信息状态
  * @access public
  */
  public function change()
  {
    $id=$this->_post($this->pkId);
    if(empty($id))$this->error('请选择操作信息');
    $funcName=$this->_get('type');
    if(method_exists($this->currentModel,$funcName))
    {
      foreach($id as $v)
      {
        $this->currentModel->$funcName(array('id'=>$v));
        $this->currentModel->$funcName(array('parent_id'=>$v));
      }
    }else{$this->error('非法操作',U(MODULE_NAME.'/index'));}
    $this->success('成功',U(MODULE_NAME.'/index'));
  }

  /**
  * 删除数据
  * @access public
  */
  public function del()
  {
    $id=$this->_request($this->pkId);
    if(is_array($id))$id=implode(',',$id);
    if($this->currentModel->delete($id)>0)
    {
      $this->currentModel->where('parent_id in('.$id.')')->delete();
      $this->success('成功',U(MODULE_NAME.'/index'));
    }else
      {
        $this->error('失败',U(MODULE_NAME.'/index'));
      }
  }    
  
}	
 ?>